<?php

/**
 *
 *	@author Smarttang <[<yucong.tang@zhaopin.com.cn>]>
 *	@date(2017.8.4) 创建
 *	@date(2017.8.9) 加入注册逻辑
 * 
 */
class User_model extends CI_Model{

	function __construct()
	{
		$this->load->helper('date');
		$this->load->database();
	}

	/**
	 * 获取所有的用户列表
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 */
	function user_list($inputs)
	{
		$this->db->select('ZL_USER_ID');
		$zl_user_count = $this->db->get('zl_users')->num_rows();

		$this->db->select('ZL_USER_ID id,ZL_USER_NAME user_name,ZL_USER_EMAIL user_email,ZL_USER_STATUS status ,ZL_USER_GROUPID groupid');
		$this->db->limit($inputs['limit'],$inputs['offset']);
		$zl_users = $this->db->get('zl_users');
		$usersarray = $zl_users->result_array();
		$count=0;
		foreach ($usersarray as $value) {
			$this->db->select('ZL_POWER_GROUP_NAME groupname');
			$this->db->where('ZL_POWER_GROUP_ID',$value['groupid']);
			$zl_group = $this->db->get('zl_power_group');
			$usersarray[$count]['groupname'] = $zl_group->row()->groupname;
			$count++;
		}

		return array(
			'status' => 1,
			'data' => $usersarray,
			'total' => $zl_user_count
		);
	}

	/**
	 * 添加用户
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 */
	function user_add($inputs)
	{
		$user_data = array(
			'ZL_USER_NAME' => $inputs['user_name'],
			'ZL_USER_PASS' => md5($inputs['password']),
			'ZL_USER_EMAIL' => $inputs['user_email'],
			'ZL_USER_STATUS' => 1,
			'ZL_USER_GROUPID' => $inputs['user_level'],
			'ZL_USER_HASHID' => $this->comon->_createRomdomKey()
		);

		$user_public = $this->db->insert('zl_users',$user_data);

		if (!$user_public){
			return array('status' => 0);
		}

		// 权限相关
		// if ($inputs['user_level'] == 2){
		// 	$priview_data = array(
		// 		'ZL_USER_HASHID' => $user_data['ZL_USER_HASHID']
		// 	);

		// 	$user_admin = $this->db->insert('zl_users_admin',$priview_data);

		// 	if (!$user_admin){
		// 		return array('status' => -1);
		// 	}
		// }

		return array('status' => 1);
	}

	/**
	 * 用户（HASHID => 名称）
	 * @return [type] [description]
	 */
	function users_select()
	{
		$this->db->select('ZL_USER_HASHID value,ZL_USER_NAME name');
		$user_data = $this->db->get('zl_users');

		if ($user_data->num_rows() > 0){
			return array(
				'status' => 1,
				'data' => $user_data->result_array()
			);
		}
		return array('status' => 0);
	}

	/**
	 * 登陆系统（查询状态、确认可用）
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 */
	function login($inputs)
	{
		$this->db->where('ZL_USER_NAME',$inputs['username']);
		$this->db->where('ZL_USER_PASS',md5($inputs['password']));
		$this->db->where('ZL_USER_STATUS',1);
		$this->db->select('ZL_USER_ID');
		$user_status = $this->db->get('zl_users')->num_rows();

		if ($user_status > 0){
			return array('status' => 1, 'data' => $inputs['username']);
		}else{
			return array('status' => 0);
		}
	}

	/**
	 * 注册用户逻辑
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 */
	function register($inputs)
	{
		$this->db->where('ZL_USER_NAME',$inputs['username']);
		$this->db->select('ZL_USER_ID');
		$user_count = $this->db->get('zl_users')->num_rows();

		if ($user_count < 1){
			// 执行注册逻辑
			// 设置一个随机密码发给用户
			return array('status' => 1);
		}else{
			// 注册失败
			return array('status' => 0);
		}
	}

	/**
	 * 用户组列表
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 */
	function groupusers_list()
	{
		$this->db->select('ZL_POWER_GROUP_NAME name,ZL_POWER_GROUP_ID id');
		$zl_users = $this->db->get('zl_power_group');

		return array(
			'status' => 1,
			'data' => $zl_users->result_array(),
		);
	}

	/**
	 * 用户删除
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 */
	function users_del($inputs)
	{
		$this->db->where('ZL_USER_NAME',$inputs['name']);
		$delete = $this->db->delete('zl_users');
		if (!$delete){
			return array('status' => -1);
		}

		return array('status' => 1);
	}
}

?>
